package main

import (
	"fmt"
	"time"
)

var ch chan string

func main() {
	//ch = make(chan string)
	//go reader()
	//fmt.Println("begin sleep")
	//time.Sleep(time.Second * 3)
	//
	//ch <- "hello"
	//time.Sleep(time.Second * 1)

	num_game()

}

func reader() {
	msg := <-ch
	fmt.Println("I am reader ", msg)
}

func num_game() {
	c1 := make(chan int)
	c2 := make(chan int)

	go func(out chan<- int) {
		for i := 0; i < 10; i++ {
			out <- i
			time.Sleep(time.Second * 1)
		}
		close(out)
	}(c1)

	go func(in <-chan int, out chan<- int) {
		for {
			num, ok := <-in
			if !ok {
				break
			}

			c2 <- num * num
		}
		close(out)
	}(c1, c2)

	for {
		num, ok := <-c2
		if !ok {
			break
		}
		fmt.Println(num)
	}

}
